package code.tools.commponents.button
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.structure.manager.language.LanguageManager;
	import code.tools.commponents.Component;
	import code.tools.commponents.Helpers;
	import code.untils.FiltersEnum;
	import code.untils.UiNameConst;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	/**
	 *带使用和添加的按钮 
	 * @author liangqian
	 * 
	 */	
	public class UseAddBtn extends Component
	{
		private var useBtnMc:MovieClip;
		private var addBtnMc:MovieClip;
		
		private var _useHandler:Function;
		private var _addFunction:Function;
		private var _addHoverFunction:Function;
		private var _addOutFunction:Function;
//		private var textformat:TextFormat;
		public function UseAddBtn(parent:DisplayObjectContainer, xpos:Number, ypos:Number,useHandler:Function,addFunction:Function,addHoverFunction:Function,addOutFunction:Function)
		{
			_useHandler = useHandler;
			_addFunction = addFunction;
			_addHoverFunction = addHoverFunction;
			_addOutFunction = addOutFunction;
			super(parent, xpos, ypos);
		}
		
		override protected function init():void
		{
			setSize(116,31);
			super.init();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
//			textformat = new TextFormat(FontManager.getInstance().embeddedFontBName);
			/* 使用   */
			useBtnMc = new(DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'useBtn')as Class)as MovieClip;
			addChild(useBtnMc);
			useBtnMc.stop();
			useBtnMc.mouseChildren = false;
			
			useBtnText = Helpers.FormatTextField(useBtnMc,4,0,useBtnMc.width,TextFieldAutoSize.CENTER,0xffffff,FontManager.getInstance().embeddedFontBName, 21);
			useBtnText.text = LanguageManager.getLanguageInfo('BTN_USE');
			/* 添加   */
			addBtnMc = new(DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'addBtn')as Class)as MovieClip;
			addBtnMc.x = useBtnMc.width - 2;
			addBtnMc.stop();
			addChild(addBtnMc);
			addBtnMc.buttonMode = addBtnMc.mouseEnabled = true;
			
			useBtnMc.addEventListener(MouseEvent.ROLL_OVER,useBtnHoverHandler);
			useBtnMc.addEventListener(MouseEvent.CLICK,useBtnHandler);
			addBtnMc.addEventListener(MouseEvent.ROLL_OVER,addBtnHoverHandler);
			addBtnMc.addEventListener(MouseEvent.CLICK,addBtnHandler);
		}
		
		override public function set enabled(value:Boolean):void
		{
			_enabled = value;
			useBtnMc.mouseEnabled = useBtnMc.buttonMode = value;
			if(value)
			{
				useBtnMc.gotoAndStop(_hover ? 2 : 1);
			}
			else
				useBtnMc.gotoAndStop(4);
			useBtnText.filters = [FiltersEnum.NO_USE_BTN_FILTERS];
		}
		
		private var _hover:Boolean;
		private function useBtnHoverHandler(e:MouseEvent):void
		{
			_hover = true;
			MySoundManager.playSound(SoundName.MOUSE_OVER, 0.2);
			useBtnMc.addEventListener(MouseEvent.ROLL_OUT,useBtnOutHandler);
			useBtnMc.gotoAndStop(2);
			useBtnText.filters = [FiltersEnum.BIG_GREEN_BTN_TEX_TFILTERS];
		}
		
		private function useBtnOutHandler(e:MouseEvent):void
		{
			_hover = false;
			useBtnMc.removeEventListener(MouseEvent.ROLL_OUT,useBtnOutHandler);
			useBtnMc.gotoAndStop(1);
			useBtnText.filters = [FiltersEnum.BIG_GREEN_BTN_TEX_TFILTERS];
		}
		
		private function useBtnHandler(e:MouseEvent):void
		{
			useBtnMc.gotoAndStop(3);
			_useHandler();
			useBtnText.filters = [FiltersEnum.BIG_GREEN_BTN_TEX_TFILTERS];
		}
		
		private function addBtnHoverHandler(e:MouseEvent):void
		{
			MySoundManager.playSound(SoundName.MOUSE_OVER, 0.2);
			addBtnMc.addEventListener(MouseEvent.ROLL_OUT,addBtnOutHandler);
			addBtnMc.gotoAndStop(2);
			_addHoverFunction(addBtnMc);
		}
		
		private function addBtnOutHandler(e:MouseEvent):void
		{
			addBtnMc.removeEventListener(MouseEvent.ROLL_OUT,addBtnOutHandler);
			addBtnMc.gotoAndStop(1);
			_addOutFunction();
		}
		
		private function addBtnHandler(e:MouseEvent):void
		{
			addBtnMc.gotoAndStop(3);
			_addFunction();
		}
		
		private var useBtnText:TextField;
	}
}